D Cookbook by 未知

D Cookbook by 未知

Author:未知
Language: eng
Format: mobi
Publisher: Packt Publishing


Tip

When writing bindings to C libraries, import core.stdc.config; and use the types c_long and c_ulong to do the correct translation. However, while this issue can be fairly easily fixed, the static assert still serves as a valuable sanity check to ensure you did do this correctly and to be a first line of defense against other translation mistakes.

Checking structure sizes is not the only thing done by static assert. It can check anything that can be run at compile time. We've used it before to verify our range objects adhere to a particular compile-time interface. Phobos' std.random also uses it to ensure parameters to a random number engine are good for pseudorandom number generation by checking the greatest common denominator and number of factors the integer parameters have.

It achieves these checks by simply writing regular functions to check the numbers and calling them in a series of static assert statements in the object as shown as follows:

struct LinearCongruentialEngine(UIntType, UIntType a, UIntType c, UIntType m) { private static bool properLinearCongruentialParameters( ulong m, ulong a, ulong c) { /* snip some implementation */ if (a == 0 || a >= m || c >= m) return false; // c and m are relatively prime if (c > 0 && gcd(c, m) != 1) return false; // a - 1 is divisible by all prime factors of m if ((a - 1) % primeFactorsOnly(m)) return false; // if a - 1 is multiple of 4, then m is a multiple of 4 too. if ((a - 1) % 4 == 0 && m % 4) return false; // Passed all tests return true; } static assert(c == 0 || properLinearCongruentialParameters(m, a, c), "Incorrect instantiation of LinearCongruentialEngine"); /* Snip remaining object implementation */ }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(56011)
What's Done in Darkness by Kayla Perrin(26588)
The Fifty Shades Trilogy & Grey by E L James(19077)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19056)
Shot Through the Heart by Mercy Celeste(18934)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17108)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16981)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16870)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16817)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16441)
The Subtle Art of Not Giving a F*ck by Mark Manson(14351)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14128)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13616)
Scorched Earth by Nick Kyme(12766)
Drei Generationen auf dem Jakobsweg by Stein Pia(10962)
Suna by Ziefle Pia(10886)
Scythe by Neal Shusterman(10333)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9519)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9365)
This is Going to Hurt by Adam Kay(9180)